<?php
require_once('conn.php');

// session_start();

//首页相应的操作
$allBoard = [];
$allArticleArr = [];
$tuijianArt = [];
$addArtBoard = [];
$bdArt = [];
$board;
/**
 * 查询所有文章
 * @return void
 */
function selectAllArticle($sort, $name = null)
{
  //搜索
  if ($name != null) {
    if ($sort == 'hot') {
      $sql = "select a.*,b.board_name,u.uName,u.avatar,(select count(*) from tbl_article_like where article_id = a.article_id) as like_count from tbl_article as a join tbl_board as b on a.board_id = b.board_id join tbl_user as u on u.uid = a.uid where a.title like '%{$name}%' or a.content like '%{$name}%' order by like_count desc;";
      $GLOBALS['allArticleArr'] = exec_select($sql);
    } elseif ($sort == 'now') {
      $sql = "select a.*,b.board_name,b.board_id,u.avatar,u.uName,(select count(*) from tbl_article_like where article_id = a.article_id) as like_count from tbl_article as a join tbl_board as b on a.board_id=b.board_id join tbl_user as u on u.uid=a.uid where a.title like '%{$name}%' or a.content like '%{$name}%' order by a.updated_time desc;";
      $GLOBALS['allArticleArr'] = exec_select($sql);
    } else {
      $sql = "select a.*,b.board_name,b.board_id,u.avatar,u.uName,(select count(*) from tbl_article_like where article_id = a.article_id) as like_count from tbl_article as a join tbl_board as b on a.board_id=b.board_id join tbl_user as u on u.uid=a.uid where a.title like '%{$name}%' or a.content like '%{$name}%' order by a.view_count desc;";
      $GLOBALS['allArticleArr'] = exec_select($sql);
    }
  } else {
    //不搜索
    if ($sort == 'hot') {
      $sql = 'select a.*,b.board_name,u.uName,u.avatar,(select count(*) from tbl_article_like where article_id = a.article_id) as like_count from tbl_article as a join tbl_board as b on a.board_id = b.board_id join tbl_user as u on u.uid = a.uid order by like_count desc;';
      $GLOBALS['allArticleArr'] = exec_select($sql);
    } elseif ($sort == 'now') {
      $sql = 'select a.*,b.board_name,b.board_id,u.avatar,u.uName,(select count(*) from tbl_article_like where article_id = a.article_id) as like_count from tbl_article as a join tbl_board as b on a.board_id=b.board_id join tbl_user as u on u.uid=a.uid order by a.updated_time desc;';
      $GLOBALS['allArticleArr'] = exec_select($sql);
    } else {
      $sql = 'select a.*,b.board_name,b.board_id,u.avatar,u.uName,(select count(*) from tbl_article_like where article_id = a.article_id) as like_count from tbl_article as a join tbl_board as b on a.board_id=b.board_id join tbl_user as u on u.uid=a.uid order by a.view_count desc;';
      $GLOBALS['allArticleArr'] = exec_select($sql);
    }
  }

}

// /**
//  * 通过搜索词查询文章
//  * @param string $name 搜索词，字符串类型
//  * @return void
//  */
// function searchArticle($name)
// {
//   $sql = "select a.*,b.board_name,b.board_id,u.avatar,u.uName,(select count(*) from tbl_article_like where article_id = a.article_id) as like_count from tbl_article as a join tbl_board as b on a.board_id = b.board_id join tbl_user as u on u.uid = a.uid where a.title like '%{$name}%' or a.content like '%{$name}%';";
//   $GLOBALS['allArticleArr'] = exec_select($sql);
//   // var_dump($GLOBALS['allArticleArr']);
// }

/**
 * 通过搜索词查询板块
 * @param string $name 搜索词，字符串类型
 * @return void
 */
function searchBoard($name)
{
  $sql = "select b.board_id,b.board_name,b.updated_time,count(a.article_id) as num from tbl_board as b left join tbl_article as a on a.board_id=b.board_id where b.board_name like '%{$name}%' group by b.board_id order by b.updated_time desc;";
  $GLOBALS['allBoard'] = exec_select($sql);
  // var_dump($GLOBALS['allBoard']);
}


/**
 * 查询所有板块
 * @return void
 */
function selectAllBoard()
{
  $sql = 'select b.board_id,b.board_name,b.updated_time,count(a.article_id) as num from tbl_board as b left join tbl_article as a on a.board_id=b.board_id group by b.board_id order by b.updated_time desc ;';
  $GLOBALS['allBoard'] = exec_select($sql);
  $GLOBALS['addArtBoard'] = exec_select($sql);
  // var_dump($GLOBALS['allBoard']);
}


/**
 * 通过用户名查询登录用户信息
 * @param string $uName 字符串类型，用户名
 * @return array  返回符合条件的用户
 */
function selectUser($uName)
{
  $sql = "SELECT * FROM `tbl_user` WHERE uName='$uName'";
  return exec_select($sql)[0];
}
function homeSelectUser($uid)
{
  $sql = "SELECT * FROM `tbl_user` WHERE uid='$uid'";
  return exec_select($sql)[0];
}


function tuijinaArticle()
{
  $sql = 'select a.*,b.board_name,b.board_id,u.avatar,u.uName,(select count(*) from tbl_article_like where article_id = a.article_id) as like_count from tbl_article as a join tbl_board as b on a.board_id=b.board_id join tbl_user as u on u.uid=a.uid order by a.view_count desc limit 6;';
  $GLOBALS['tuijianArt'] = exec_select($sql);
}


// 通过板块来查找文章
function boardArt($bid)
{
  $bdSql = "select * from tbl_board where board_id = $bid;";
  $GLOBALS['board'] = exec_select($bdSql)[0];
  $sql = "select a.*,b.board_name,u.uName,u.avatar,(select count(*) from tbl_article_like where article_id = a.article_id) as like_count from tbl_article as a join tbl_board as b on a.board_id = b.board_id join tbl_user as u on u.uid = a.uid where a.board_id=$bid order by a.updated_time desc;";
  $GLOBALS['bdArt'] = exec_select($sql);
}


function viewCountAdd($aid)
{
  $sql = "select * from tbl_article where article_id = $aid";
  $art = exec_select($sql)[0];
  if (count($art) > 0) {
    $oldView = intval($art['view_count']);
    $newView = $oldView + 1;
    // echo $newView;
    $upSql = "update tbl_article  set view_count =$newView where article_id =$aid ;";
    // echo $upSql;
    exec_update($upSql);
  }
}


function dzCountAdd($aid)
{
  $sql = "select * from tbl_article where article_id = $aid";
  $art = exec_select($sql)[0];
  if (count($art) > 0) {
    if (isset($_SESSION['uName']) && trim($_SESSION['uName']) != '') {
      $sqlU = "select uid from tbl_user where uName ='$_SESSION[uName]'";
      $userId = exec_select($sqlU)[0]['uid'];
      $upSql = "insert into tbl_article_like value(null,$userId,$aid)";
      exec_update($upSql);
    } else {
      setcookie('err', 'yes');
    }
  }
}
?>